<?php
namespace app\index\controller;

use think\Db;

/**
 * 订单支付控制器
 */
class Order extends \think\Controller
{
  public $pdata; //订单请求参数格式：{"method":"wechat.mp", "data":{"out_trade_no":"KFwCTCMraLunc4DPX","user_id":"qwerasdf","super_agency_id":"zxcvasdf"}}

  //返回订单详情和支付方式选择页面
  public function index()
  {
    $this->pdata = $_GET['pdata'] ? $_GET['pdata'] : '';

    if ( empty($this->pdata) ){
      return "pdata empty";
    }

    $pdata = json_decode($this->pdata, true);

    $pdata['method'] = explode(".", $pdata['method']);

    $pdata['driver'] = $pdata['method'][0];

    $pdata['gateway'] = $pdata['method'][1];


    $data = $this->preOrder($pdata);

    return $this->fetch($pdata['driver'].$pdata['gateway'], $data);

  }

  //订单预处理，如微信公众号支付获取openid
  public function preOrder($pdata){
    $class = 'app\index\controller\\'. $pdata['driver'] . '\\' . ucfirst($pdata['driver']) . ucfirst($pdata['gateway']);
    $order = new $class($pdata);

    return $order->preOrder($pdata);
  }


  //获取订单详情
  public function queryOrder($out_trade_no){
    $order_data = Db::name('orders')->where(['_id' => $out_trade_no])->find();
    return $order_data;
  }

  //获取商品信息
  public function queryProduct($product_id){
    $product_data = Db::name('cards')->where(['_id' => $product_id])->find();
    return $product_data;
  }

}
